﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WMS.BLL;
using WMS.Entity;

namespace WMS.App
{
    public partial class frmUnit : Form
    {
        #region Private

        private Unit unit = null;
        private ClickAction _Action;

        #endregion

        #region Public Event
        public ResultEventHandler ResultCallBackEvent = null;
        #endregion

        public frmUnit()
        {
            InitializeComponent();

            InitData();
        }

        void InitData()
        {
            #region
            SetComponetEnabled(true);

            unit = new Unit();
            IList<UnitEntity> allUnit = unit.GetAllUnit();

            this.dataGridView1.DataSource = allUnit;
            #endregion
        }

        #region Button Click

        private void frmUnit_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Visible == true)
            {
                e.Cancel = true;

                this.tsExit_Click(null, null);

                return;
            }
        }

        private void tsExit_Click(object sender, EventArgs e)
        {
            this.Hide();
            if (ResultCallBackEvent != null)
            {
                ResultCallBackEvent(this, ResultEventArgs.Empty);
            }
        }

        private void Button_Click(object sender, EventArgs e)
        {
            #region
            ToolStripButton b = ((ToolStripButton)sender);

            switch (b.Name)
            {
                case "tsAdd":
                    _Action = ClickAction.Add;
                    SetComponetEnabled(false);
                    this.txtUnitName.Text = "";
                    this.txtUnitName.Focus();
                    break;
                case "tsModify":
                    _Action = ClickAction.Modify;
                    SetComponetEnabled(false);
                    this.txtUnitName.Focus();
                    break;
                case "tsDelete":
                    if (MessageBoxCommon.ConfirmDelete())
                    {
                        if (this.dataGridView1.CurrentRow != null)
                        {
                            unit.DeleteUnitBy(int.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString()));

                            InitData();
                        }
                    }
                    break;
                default: break;
            }
            #endregion
        }

        private void tsSave_Click(object sender, EventArgs e)
        {
            OperateData();
        }

        private void tsCancel_Click(object sender, EventArgs e)
        {
            SetComponetEnabled(true);
            this.dataGridView1.Refresh();
        }

        private void tsRefresh_Click(object sender, EventArgs e)
        {
            SetComponetEnabled(true);
            InitData();
        }

        #endregion

        #region Data
        /// <summary>
        /// 增删改查等操作
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        void OperateData()
        {
            if (ValidateUnitName())
            {
                UnitEntity entity = new UnitEntity();
                if (_Action == ClickAction.Add)
                {
                    entity.UnitName = this.txtUnitName.Text.Trim();
                    unit.AddUnit(entity);

                    InitData();
                    this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].Selected = true;

                }
                else
                {
                    entity.UnitName = this.txtUnitName.Text.Trim();
                    entity.UnitID = int.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());

                    unit.UpdateUnit(entity);
                    this.dataGridView1.CurrentRow.Cells[1].Value = entity.UnitName;
                }
            }

            SetComponetEnabled(true);
        }

        bool ValidateUnitName()
        {
            var unitName = this.txtUnitName.Text.Trim();

            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                if (row.Cells["UnitName"].Value.ToString().Trim() == unitName)
                {
                    return false;
                }
            }

            return true;
        }

        #endregion

        #region Componnet Enabled
        void SetComponetEnabled(bool value)
        {
            this.tsAdd.Enabled = value;
            this.tsModify.Enabled = value;
            this.tsDelete.Enabled = value;
            this.tsRefresh.Enabled = value;
            this.tsExit.Enabled = value;
            this.dataGridView1.Enabled = value;

            this.tsSave.Enabled = !value;
            this.tsCancel.Enabled = !value;
            this.txtUnitName.Enabled = !value;
        }
        #endregion

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            this.txtUnitName.Text = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
        }
    }
}

